package com.aurora.CETManagementSystem.controller;


import com.aurora.CETManagementSystem.mapper.NewsMapper;
import com.aurora.CETManagementSystem.pojo.News;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.io.FileUtils;
import org.springframework.jca.endpoint.GenericMessageEndpointManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aurora
 * @since 2021-02-18
 */
@Controller
public class NewsController {

    @Resource
    NewsMapper newsMapper;


    /**
     * 根据标题或者正文对新闻进行检索
     * @param search
     * @param model
     * @return
     */
    @GetMapping("/student/news/search")
    public String newsSearch(String search,Model model){
        System.out.println("search = " + search + ", model = " + model);
        List<News> news = newsMapper.selectList(new QueryWrapper<News>().like("news_title", search).or().like("news_info", search));

        if(!news.isEmpty()){
            model.addAttribute("news", news);
        }else {
            model.addAttribute("msg", "未查询到相关结果!");
        }


        return "student/information";


    }


    /**
     * 添加资讯,上传图片
     * @param file 图片文件
     * @return
     */
    @PostMapping("/admin/news/add")
    @ResponseBody
    public String addAttraction(News news, MultipartFile file) throws IOException {

        News oldnew = newsMapper.selectOne(new QueryWrapper<News>().eq("news_title", news.getNewsTitle()));
        if(oldnew!=null){
            return "已经存在相同标题的资讯！";
        }

        if (file.isEmpty()) {
            return "添加失败，请上传照片.";
        }



        String fileName = UUID.randomUUID()+file.getOriginalFilename();
        String filePath = System.getProperty("user.dir")+"/cetImg_upload/";
        //String filePath = System.getProperty("user.dir")+"/target/classes/static/asserts/img_upload/";
        //String devfilePath = System.getProperty("user.dir")+"/src/main/resources/static/asserts/img_upload/";
        File dest = new File(filePath + fileName);
        //File devDest = new File(devfilePath + fileName);




        try {
            //FileUtils.copyInputStreamToFile(file.getInputStream(),devDest);
            FileUtils.copyInputStreamToFile(file.getInputStream(),dest);
            news.setNewsPicture(fileName);
            news.setCreateTime(new Date());
            news.setUpdateTime(new Date());

            int result = newsMapper.insert(news);
            if(result==1){
                return "上传成功！";
            }else {
                return "上传失败！";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败！系统错误！";
        }

    }



    @GetMapping("/admin/news/delete/{id}")
    public String deleteNews(@PathVariable("id") String id){

        newsMapper.deleteById(Integer.parseInt(id));

        return "redirect:/admin/news";
    }


}

